package cn.com.headfree.point.service.impl;

import cn.com.headfree.entity.TPointRuleInfo;
import cn.com.headfree.entity.TRuleElementInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.point.dao.PointRuleDao;
import cn.com.headfree.point.dao.RuleElementDao;
import cn.com.headfree.point.service.PointRuleService;
import com.gta.edu.sdk.util.IdGen;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gta.edu.sdk.common.dto.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("poingRuleService")
@Transactional
public class PointRuleServiceImpl implements PointRuleService {

	private static final Logger log = LogManager.getLogger(PointRuleServiceImpl.class);

	@Autowired
	private PointRuleDao pointRuleDao;

	@Autowired
	private RuleElementDao ruleElementDao;

	/**
	 * 分页查询
	 * @param page
	 * @param pointRuleInfo
	 */
	@Override
	public void getPagePointRuleInfo(Page<TPointRuleInfo> page,TPointRuleInfo pointRuleInfo,User user) {
		pointRuleDao.getPagePoingRuleInfo(page,pointRuleInfo,user);
	}

	/**
	 * 保存
	 * @param pointRuleInfo
	 */
	@Override
	public void savePointRuleInfo(TPointRuleInfo pointRuleInfo) {
		pointRuleDao.savePointRuleInfo(pointRuleInfo);
	}

	/**
	 * 规则启用
	 * @param ruleId
	 * @param user
	 */
	@Override
	public void useRuleInfo(String ruleId, User user) {
		//先将之前状态为启用的数据修改为未启用状态
		TPointRuleInfo pointRuleInfo = new TPointRuleInfo();
		pointRuleInfo.setUpdaterId(user.getFax());
		pointRuleInfo.setUpdaterName(user.getName());
		pointRuleInfo.setOrgId(user.getBaseOrgId());
		pointRuleDao.updatePointRuleInfoByState(pointRuleInfo);
		pointRuleInfo.setId(ruleId);
		pointRuleDao.updatePointRuleInfoById(pointRuleInfo);
	}

	/**
	 * 一键复用
	 * @param pointRuleInfo
	 * @param user
	 */
	@Override
	public void oneKeyCopyInfo(TPointRuleInfo pointRuleInfo, User user) {
		//获取所有加分项
		List<TRuleElementInfo> ruleElementInfos = ruleElementDao.findRuleElementListByRuleId(pointRuleInfo.getId());
		//保存规则主表
		pointRuleInfo.setId(IdGen.uuid());
		pointRuleInfo.setCreaterId(user.getFax());
		pointRuleInfo.setCreaterName(user.getName());
		pointRuleInfo.setCreateTime(new Date());
		pointRuleInfo.setUpdaterId(user.getFax());
		pointRuleInfo.setUpdaterName(user.getName());
		pointRuleInfo.setUpdateTime(new Date());
		pointRuleInfo.setState(0);
		pointRuleInfo.setDelSign(0);
		pointRuleDao.savePointRuleInfo(pointRuleInfo);
		//保存规则加分项
		if(ruleElementInfos != null && ruleElementInfos.size() > 0){
			for(TRuleElementInfo ruleElementInfo:ruleElementInfos){
				ruleElementInfo.setId(IdGen.uuid());
				ruleElementInfo.setRuleId(pointRuleInfo.getId());
				ruleElementInfo.setYear(pointRuleInfo.getYear());
				ruleElementInfo.setCreaterId(user.getFax());
				ruleElementInfo.setCreaterName(user.getName());
				ruleElementInfo.setCreateTime(new Date());
				ruleElementInfo.setUpdaterId(user.getFax());
				ruleElementInfo.setUpdaterName(user.getName());
				ruleElementInfo.setUpdateTime(new Date());
				ruleElementDao.saveRuleElementInfo(ruleElementInfo);
			}
		}
	}

	/**
	 * 获取当前启用的规则(只会有一条被启用)
	 */
	public List<TPointRuleInfo> findStartUsePointRuleInfoList(String orgId){
		return pointRuleDao.findStartUsePointRuleInfoList(orgId);
	}

}
